//
//  MainViewController.m
//  BJS151148_ TeamProject_Travel
//
//  Created by lanou3g on 16/3/21.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "MainViewController.h"
/**滑动标题栏*/
#import "UIView+Extension.h"

#import "StrategyViewController.h"
#import "ToolsViewController.h"
#import "SpecialTableViewController.h"
#import "OfflineTableViewController.h"
#import "LCViewController.h"

#import "FeedbackViewController.h"
#import "SetTableViewController.h"

/**加载动画*/
#import "LBProgressHUD.h"

/**下拉菜单*/
#import "PellTableViewSelect.h"
#import "LoginViewController.h"
#pragma mark - 宏定义

#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
/** 文字高度  */
static CGFloat const titleH = 44;
/** 选中文字放大  */
static CGFloat const MaxScale = 1.2;

@interface MainViewController ()<UIScrollViewDelegate>
/** 文字scrollView  */
@property (nonatomic, strong) UIScrollView *titleScrollView;
/** 控制器scrollView  */
@property (nonatomic, strong) UIScrollView *contentScrollView;
/** 标签文字  */
@property (nonatomic ,strong) NSArray * titlesArr;
/** 标签按钮  */
@property (nonatomic, strong) NSMutableArray *buttons;
/** 选中的按钮  */
@property (nonatomic ,strong) UIButton * selectedBtn;
/** 选中的下划线  */
@property (nonatomic ,strong) UIView *backView;
@end

@implementation MainViewController

- (void)viewWillAppear:(BOOL)animated{
    [LBProgressHUD showHUDto:self.view animated:YES];
}

- (void)viewDidAppear:(BOOL)animated{
    [LBProgressHUD hideAllHUDsForView:self.view animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"随心·自由行";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0 green:191/255.0 blue:255/255.0 alpha:1];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-ziyouxing"] style:UIBarButtonItemStyleDone target:self action:nil];
    UIBarButtonItem *right2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-dian-3"] style:UIBarButtonItemStyleDone target:self action:@selector(setAction)];
    UIBarButtonItem *right1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-geren2-2"] style:UIBarButtonItemStyleDone target:self action:@selector(loginAction)];
    self.navigationItem.rightBarButtonItems = @[right2,right1];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    [self addChildViewController];    /** 添加子控制器视图  */
    
    [self setTitleScrollView];        /** 添加文字标签  */
    
    [self setContentScrollView];      /** 添加scrollView  */
    
    [self setupTitle];                /** 设置标签按钮 文字 背景图  */
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.contentScrollView.contentSize = CGSizeMake(self.titlesArr.count * ScreenW, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.showsHorizontalScrollIndicator  = NO;
    self.contentScrollView.delegate = self;

    
}
- (void)loginAction{

    LoginViewController *loginVC = [LoginViewController new];
    [self.navigationController pushViewController:loginVC animated:YES];
}
#pragma mark lazy loading
- (NSMutableArray *)buttons
{
    if (!_buttons)
    {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}


-(NSArray *)titlesArr
{
    
    if (!_titlesArr) {
        _titlesArr  = [NSArray arrayWithObjects:@"游  记",@"攻  略",@"工具箱",@"专  题",@"离线浏览",nil];
    }
    
    return _titlesArr;
}
#pragma mark - PRIVATE

-(void)addChildViewController{

    LCViewController *vc  = [[LCViewController alloc] init];
    vc.title  =  self.titlesArr[0];
    [self addChildViewController:vc];

    StrategyViewController *StraVC = [[StrategyViewController alloc]init];
        StraVC.title = self.titlesArr[1];
        [self addChildViewController: StraVC];
    
    ToolsViewController *vc1  = [[ToolsViewController alloc] init];
    vc1.title  =  self.titlesArr[2];
    [self addChildViewController:vc1];
    
    
    SpecialTableViewController *vc2  = [[SpecialTableViewController alloc] init];
    vc2.title  =  self.titlesArr[3];
    [self addChildViewController:vc2];
    
    
    OfflineTableViewController *vc3  = [[OfflineTableViewController alloc] init];
    vc3.title  =  self.titlesArr[4];
    [self addChildViewController:vc3];
    }
-(void)setTitleScrollView{
    
    CGRect rect  = CGRectMake(8, 64, ScreenW, titleH);
    self.titleScrollView = [[UIScrollView alloc] initWithFrame:rect];
    [self.view addSubview:self.titleScrollView];
    
}
-(void)setContentScrollView{
    
    CGFloat y  = CGRectGetMaxY(self.titleScrollView.frame);
    CGRect rect  = CGRectMake(0, y, ScreenW, ScreenH - titleH);
    self.contentScrollView = [[UIScrollView alloc] initWithFrame:rect];
    [self.view addSubview:self.contentScrollView];
    
    
}

-(void)setupTitle{
    NSUInteger count = self.childViewControllers.count;
    
    CGFloat x = 0;
    CGFloat w = 80;
    CGFloat h = titleH;
    
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(5, 40, 70, 3)];
    self.backView.backgroundColor = [UIColor colorWithRed:0 green:191/255.0 blue:255/255.0 alpha:1];
    [self.titleScrollView addSubview:self.backView];
    
    for (int i = 0; i < count; i++)
    {
        UIViewController *vc = self.childViewControllers[i];
        
        x = i * w;
        CGRect rect = CGRectMake(x, 0, w, h);
        UIButton *btn = [[UIButton alloc] initWithFrame:rect];
        
        btn.tag = i;
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        
        
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchDown];
        
        [self.buttons addObject:btn];
        [self.titleScrollView addSubview:btn];
        
        
        if (i == 0)
        {
            [self click:btn];
        }
        
    }
    self.titleScrollView.contentSize = CGSizeMake(count * w, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    
}
-(void)click:(UIButton *)sender{
    
    [self selectTitleBtn:sender];
    NSInteger i = sender.tag;
    CGFloat x  = i *ScreenW;
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
    
    [self setUpOneChildController:i];
    
    
}

-(void)selectTitleBtn:(UIButton *)btn{
    
    
    [self.selectedBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.selectedBtn.transform = CGAffineTransformIdentity;
    
    
    [btn setTitleColor:[UIColor colorWithRed:0 green:191/255.0 blue:255/255.0 alpha:1] forState:UIControlStateNormal];
    btn.transform = CGAffineTransformMakeScale(MaxScale, MaxScale);
    self.selectedBtn = btn;
    
    [self setupTitleCenter:btn];
    
}

-(void)setupTitleCenter:(UIButton *)sender
{
    
    //    CGFloat offset = sender.center.x - ScreenW * 0.5;
    //    if (offset < 0) {
    //        offset = 0;
    //    }
    //    CGFloat maxOffset  = self.titleScrollView.contentSize.width - ScreenW;
    //    if (offset > maxOffset) {
    //        offset = maxOffset;
    //    }
    //    [self.titleScrollView setContentOffset:CGPointMake(offset+5, 0) animated:YES];
    
}

-(void)setUpOneChildController:(NSInteger)index{
    
    
    CGFloat x  = index * ScreenW;
    UIViewController *vc  =  self.childViewControllers[index];
    if (vc.view.superview) {
        return;
    }
    vc.view.frame = CGRectMake(x, 0, ScreenW, ScreenH - self.contentScrollView.frame.origin.y);
    [self.contentScrollView addSubview:vc.view];
    
}


#pragma mark - UIScrollView  delegate

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    NSInteger i  = self.contentScrollView.contentOffset.x / ScreenW;
    [self selectTitleBtn:self.buttons[i]];
    [self setUpOneChildController:i];
    
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offsetX  = scrollView.contentOffset.x;
    NSInteger leftIndex  = offsetX / ScreenW;
    NSInteger rightIdex  = leftIndex + 1;
    
    UIButton *leftButton = self.buttons[leftIndex];
    UIButton *rightButton  = nil;
    if (rightIdex < self.buttons.count) {
        rightButton  = self.buttons[rightIdex];
    }
    CGFloat scaleR  = offsetX / ScreenW - leftIndex;
    CGFloat scaleL  = 1 - scaleR;
    CGFloat transScale = MaxScale - 1;
    
    self.backView.transform  = CGAffineTransformMakeTranslation((offsetX*(self.titleScrollView.contentSize.width / self.contentScrollView.contentSize.width)), 0);
    
    leftButton.transform = CGAffineTransformMakeScale(scaleL * transScale + 1, scaleL * transScale + 1);
    rightButton.transform = CGAffineTransformMakeScale(scaleR * transScale + 1, scaleR * transScale + 1);
    
    [leftButton setTitleColor:[UIColor colorWithRed:0 green:191/255.0 blue:255/255.0 alpha:1] forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
}

- (void)setAction{

    [PellTableViewSelect addPellTableViewSelectWithWindowFrame:CGRectMake(self.view.bounds.size.width-100, 64, 150, 200) selectData:@[@"反  馈",@"设  置"] images:@[@"iconfont-fankui.png",@"iconfont-shezhi.png"] action:^(NSInteger index) {
        
        if(index == 0){
        
            FeedbackViewController *feedVC = [[FeedbackViewController alloc] init];
            [self.navigationController pushViewController:feedVC animated:YES];
        }else{
        
            SetTableViewController *setVC = [[SetTableViewController alloc]init];
            [self.navigationController pushViewController:setVC animated:YES];
        }
        
    } animated:YES];
}


@end
